package yxy.game.pm2.command.kow;

import yxy.cherry.data.script.fomula;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.PlayerBean;
import yxy.game.pm2.bean.ServerBean;
import yxy.game.pm2.bean.area.AreaBean;
import yxy.game.pm2.bean.currency.CurrencyNotEnoughException;
import yxy.game.pm2.bean.currency.CurrencyType;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.KowMessage;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.kow.KowHandle;
import yxy.game.pm2.module.kow.KowModule;
import yxy.game.pm2.module.kow.KowPlayerModule;
import yxy.game.pm2.module.kow.RankKowGroup;

/** 天下第一 天下第一小组赛刷新对手 */
@MessageType(proto = Command.KowGroupRefresh, classtype = KowMessage.KowGroupRefreshRequest.class)
public class CMD_KowGroupRefresh extends Command<KowMessage.KowGroupRefreshRequest> {

	@Override
	public void execute(MessageSource messageSource, KowMessage.KowGroupRefreshRequest request) {
		String playerCode = messageSource.getTag();

		KowMessage.KowGroupRefreshResponse.Builder response = KowMessage.KowGroupRefreshResponse.newBuilder();

		PlayerBean playerBean = PlayerBean.get(playerCode);
		ServerBean serverBean = ServerBean.get(playerBean.getServerId());
		AreaBean areaBean = AreaBean.get(serverBean.getAreaId());

		KowModule kowModule = KowModule.get(areaBean.getGroupId());
		KowPlayerModule kowPlayerModule = KowPlayerModule.get(playerCode);
		if (!kowPlayerModule.getCode().equals(kowModule.getCode())) {
			send(messageSource, response, Result.KowNoIn);
			return;// 未参赛
		}
		RankKowGroup rankKowGroup = new RankKowGroup(areaBean.getGroupId(), kowPlayerModule.getGroup());

		try {
			if (kowPlayerModule.tryLock()) {
				int costNum = fomula.天下第一积分赛刷新对手花费元宝.intValue(kowPlayerModule.getRefreshtimes());
				try {
					new CurrencyHandle().cost(playerCode, CCReason.天下第一, Currency.create(CurrencyType.CT_元宝, 0, costNum));
				} catch (CurrencyNotEnoughException e) {
					send(messageSource, response, new Result(Result.CostFail, e.getType().getNumber(), e.getDataId()));// 消费不足
					return;
				}
				kowPlayerModule.setRefreshtimes(kowPlayerModule.getRefreshtimes() + 1);
				KowHandle kowHandle = new KowHandle();
				kowHandle.refreshOppo(kowPlayerModule, rankKowGroup);

				response.addAllOppos(kowHandle.oppos(kowPlayerModule, rankKowGroup));
				send(messageSource, response, Result.Success);
			}
		} catch (Exception e) {
			e.printStackTrace();
			send(messageSource, response, Result.DataError);
		} finally {
			kowPlayerModule.unlock();
		}
	}

	private void send(MessageSource messageSource, KowMessage.KowGroupRefreshResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}